package com.lilicould.controller;

import com.lilicould.entity.ContractTenant;
import com.lilicould.service.impl.ContractServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/contract/addTenant")
public class ContractAddTenantController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String contractId = req.getParameter("contractId");
        String tenantId = req.getParameter("tenantId");

        // 封装
        ContractTenant contractTenant = new ContractTenant(contractId, tenantId);

        // 调用业务逻辑层
        ContractServiceImpl contractService = new ContractServiceImpl();

        boolean result = contractService.addContractTenant(contractTenant);

        if (result) {
            req.setAttribute("success", "添加成功");
            req.getRequestDispatcher("/page/success.jsp").forward(req, resp);
        } else {
            req.setAttribute("error", "添加失败，请检查租客编号是否正确或该合同是否已存在该租客信息");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
        }

    }
}
